package edu_admin;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;

import javax.xml.parsers.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;


public class eduxml {
	private Document document;
	String[] title1,date1,content1;
	
	public eduxml() throws ParserConfigurationException{
		Href_Title_Time htt=new Href_Title_Time();
		htt.getparts(htt.usefulcode);
		title1=htt.title;
		date1=htt.time;
		                        
		getContent gt=new getContent();
		content1=gt.getContext(gt.href);
		
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		
		document=builder.newDocument();
		
	}
	
	public Document toWrite(String[] tit,String[] tim,String[] com){
			Element root=document.createElement("result");
			document.appendChild(root);                                        //<result>
			Element info=document .createElement("info");
			root.appendChild(info);                                               //<result>-<info>
			Element category=document .createElement("category");
			category.appendChild(document.createTextNode("edu_admin_notice"));
			info.appendChild(category);                                           //<result>-<info>-<category>
			Element update=document.createElement("update");
			Date now=new Date();
	    	DateFormat d = DateFormat.getDateTimeInstance();                      //<result>-<info>-<update>
	    	String t = d.format(now);
	    	update.appendChild(document.createTextNode(t));
	    	info.appendChild(update);
	    	
	    	Element list=document .createElement("list");                      
			root.appendChild(list);                                               //<result>-<list>
			
		    for(int i=0;i<20;i++){
			    Element item=document .createElement("item");                      
			    list.appendChild(item);                                               //<result>-<list>-<item>
			    Element id=document .createElement("id");                      
			    id.appendChild(document.createTextNode(Integer.toString(i+1)));
			    item.appendChild(id);                                                 //<result>-<list>-<item>-<id>
			    Element isread=document .createElement("isread");                      
			    isread.appendChild(document.createTextNode("false"));
			    item.appendChild(isread);                                             //<result>-<list>-<item>-<isread>
			    Element title=document .createElement("title");                      
			    title.appendChild(document.createTextNode(tit[i]));
			    item.appendChild(title);                                              //<result>-<list>-<item>-<title>
			    Element date =document .createElement("date");                      
			    date.appendChild(document.createTextNode(tim[i]));
			    item.appendChild(date);
			    Element target_date=document .createElement("target_date");
			
			    //taget_date空着！！！！！
			
			    item.appendChild(target_date);
			    com[i]=com[i].replace(tit[i], "");
			    com[i]=com[i].replaceFirst("【([^】]*)】", "");
                Element intro=document .createElement("intro");
			    String s=com[i].substring(0, 39)+"...";
			    intro.appendChild(document.createTextNode(s));
			    item.appendChild(intro);
			    Element content =document .createElement("content");
			    content.appendChild(document.createTextNode(com[i]));
			    item.appendChild(content);                                           //<result>-<list>-<item>-<content>
			
		    }
		
		 return document;
			
		}
	
	
	public void toSave(Document d) throws FileNotFoundException{
		
	
			try {
		    TransformerFactory tf=TransformerFactory.newInstance();
			Transformer transformer=tf.newTransformer();
			DOMSource source=new DOMSource(d);
		    transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
		    PrintWriter pw;
			
		    pw = new PrintWriter(new FileOutputStream("edu_admin.xml"));
		
		    StreamResult result=new StreamResult(pw);
		    try {
				transformer.transform(source, result);
			} catch (TransformerException e) {
				e.printStackTrace();
			}
		    
			} catch (TransformerConfigurationException e) {
			    e.printStackTrace();
		    }
		
	    
	}
	
	
		

}
